﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PerfumeShopManagement.BLL;
using PerfumeShopManagement.Utility;
using PerfumeShopManagement.Model;
using System.Threading;

namespace PerfumeShopManagement.View
{
    public partial class frmImportCheck : Form
    {
        private BllImportCheck bllImportCheck;

        public frmImportCheck(BllImportCheck bllImportCheck)
        {
            this.bllImportCheck = bllImportCheck;
            InitializeComponent();
        }

        private void doAutoDateTime()
        {
            //start the time running
            backgroundWorker.RunWorkerAsync();
        }

        private void setTotal()
        {
            float total = 0.0f;

            if (gridProducts.Rows.Count > 1)
            {
                for (int i = 0; i < gridProducts.Rows.Count - 1; i++)
                {
                    DataGridViewRow item = gridProducts.Rows[i];
                    try
                    {
                        int amount = int.Parse(item.Cells["colAmount"].Value.ToString());
                        float price = float.Parse(item.Cells["colPrice"].Value.ToString());
                        total += amount * price;
                    }
                    catch { } // Continue
                }
            }

            lblTotal.Text = total.ToString();
        }

        private void setId()
        {
            lblImportCheckId.Text = Constants.AUTO_NUMBERING_MESSAGE;
        }

        private void frmImportCheck_Load(object sender, EventArgs e)
        {
            setId();
            doAutoDateTime();
        }

        private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            while (true)
            {
                Thread.Sleep(500);
                if (lblImportDate != null)
                {
                    if (!lblImportDate.InvokeRequired)
                    {
                        DateTime now = DateTime.Now;
                        lblImportDate.Text = String.Format("{0:dd/MM/yyyy HH:mm:ss}", now);
                    }
                }
            }
        }

        private void btnSaveImportCheck_Click(object sender, EventArgs e)
        {
            // Creates ExportCheck Object for Saving
            ImportCheck importCheck = new ImportCheck();
            if (NumberUtil.IsNumeric(lblImportCheckId.Text))
                importCheck.ImportCheckId = int.Parse(lblImportCheckId.Text);
            importCheck.ImportCheckTime = DateTime.Now;
            if (NumberUtil.IsNumeric(lblTotal.Text))
                importCheck.Total = double.Parse(lblTotal.Text);

            for (int i = 0; i < gridProducts.RowCount - 1; i++)
            {
                ImportCheckDetail importCheckDetail = new ImportCheckDetail();

                DataGridViewRow item = gridProducts.Rows[i];
                if (NumberUtil.IsNumeric(item.Cells["colProductId"].Value))
                    importCheckDetail.ProductId = int.Parse(item.Cells["colProductId"].Value.ToString());
                if (NumberUtil.IsNumeric(item.Cells["colAmount"].Value))
                    importCheckDetail.ImportCheckAmount = int.Parse(item.Cells["colAmount"].Value.ToString());

                importCheck.ImportCheckDetail.Add(importCheckDetail);
            }

            // Executes saving
            if (bllImportCheck.Save(importCheck))
            {
                MessageBox.Show(Constants.SUCCESSFULLY_MESSAGE);
                this.Close();
            }
            else
            {
                MessageBox.Show(Constants.FAILED_MESSAGE);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void gridProducts_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            setTotal();
        }
    }
}
